package jp.co.google.ben.picasa.server;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import jp.co.google.ben.picasa.client.AlbumService;
import jp.co.google.ben.picasa.model.AlbumModel;
import jp.co.google.ben.picasa.model.PhotoModel;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.repackaged.org.apache.commons.logging.Log;
import com.google.appengine.repackaged.org.apache.commons.logging.LogFactory;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class AlbumServiceImpl extends RemoteServiceServlet implements
		AlbumService {

	private final static Log log = LogFactory.getLog(AlbumServiceImpl.class);
	private static PicasawebService service;
	
	
	public void loginGoogle(){
		log.debug("loginGoogle start");
		log.debug("loginGoogle end");
	}
	
	public List<AlbumModel> loadAlbum(){
		log.debug("loadAlbum start");
		
		
		List<AlbumModel> albumList = new ArrayList<AlbumModel>();
		try{
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			initPicasawebService();
			URL feedUrl;
			feedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/" + user.getNickname() + "?kind=album");
			UserFeed myUserFeed = service.getFeed(feedUrl, UserFeed.class);
			List<AlbumEntry> albumEntryList = myUserFeed.getAlbumEntries();
		    AlbumEntry albumEntry = null;
		    AlbumModel albumModel = null;
		    for (int i=0;i<albumEntryList.size();i++) {
	        	albumEntry = albumEntryList.get(i);
	        	albumModel = new AlbumModel();
	        	albumModel.setAlbumId(albumEntry.getGphotoId());
	        	albumModel.setAlbumName(albumEntry.getTitle().getPlainText());
	        	albumModel.setThumbnailUrl(albumEntry.getMediaThumbnails().get(0).getUrl());
	        	albumModel.setAlbumSize(albumEntry.getPhotosUsed());
	        	albumList.add(albumModel);
	        }
		}catch(Exception e){
			log.error(e);
		}
		log.debug("loadAlbum end");
		return albumList;
	}

	public List<PhotoModel> loadPhoto(String albumId,int pageIndex) throws IllegalArgumentException {
		log.debug("loadPhoto start");
		int pageSize = 16;
		List<PhotoModel> photoList = new ArrayList<PhotoModel>();
		// TODO Auto-generated method stub
		try{
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			initPicasawebService();
			URL feedUrl = new URL("http://picasaweb.google.com/data/feed/api/user/" + user.getNickname() + "/albumid/" + albumId);
			AlbumFeed myUserFeed = service.getFeed(feedUrl, AlbumFeed.class);
			List<PhotoEntry> photoEntryList = myUserFeed.getPhotoEntries();
			log.debug("photoEntryList size :" + photoEntryList.size());
			if(photoEntryList.size() > pageSize){
				log.debug("photoEntryList size :" + photoEntryList.size());
				photoEntryList = photoEntryList.subList(0, pageSize);
				log.debug("photoEntryList size :" + photoEntryList.size());
			}
			PhotoModel photoModel = null;
			for(PhotoEntry photoEntry : photoEntryList){
				photoEntry.getAdaptedEntry();
				photoModel = new PhotoModel();
				photoModel.setPhotoTitle(photoEntry.getTitle().getPlainText());
				photoModel.setThumbUrl(photoEntry.getMediaThumbnails().get(1).getUrl());
				log.debug("ThumbUrl" + photoModel.getThumbUrl());
				photoModel.setUrl(photoEntry.getMediaGroup().getContents().get(0).getUrl());
				photoList.add(photoModel);
			}
		
		}catch(Exception e){
			log.error(e);
		}
		
		log.info("photoList size : " + photoList.size());
		log.debug("loadPhoto end");
		return photoList;
	}
	
	public static void initPicasawebService(){
		if(service == null){
			service = new PicasawebService("mypicasa");
		}
	} 
}
